package com.heima;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

/**
 * Driud数据库连接池的使用
 * 1. 导入jar包 druid-1.1.12.jar
 * 2. 定义配置文件
 * 3. 加载配置文件
 * 4. 获取数据库连接池对象
 * 5. 获取连接
 */

public class JDBC_DriudDemo1 {

    @Test
    public void sPreparedStatement_login() throws Exception {
        //1. 导入jar包 druid-1.1.12.jar

        //2.定义配置文件

        //3.加载配置文件
        Properties prop=new Properties();
        System.out.println(System.getProperty("user.dir"));
        prop.load(new FileInputStream("注解开发控制bean生命周期/druid.properties"));

        //4. 获取数据库连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

        //5.获取链接
        Connection conn = dataSource.getConnection();
        System.out.println(conn);



        String name="zhangsan";
        String pw="123";
        //6.定义sql
        String sql="select *from user where username=? and password =?";

        //预编译执行SQL
        PreparedStatement pstmt = conn.prepareStatement(sql);
        //设置？占位符的值
        pstmt.setString(1,name);
        pstmt.setString(2,pw);
        //执行SQL
        ResultSet rs = pstmt.executeQuery();

        //遍历结果集，判断是否登录成功
        if (rs.next()){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }



//
//    }


    }
}
